<?php
/**
 * -----------------------------------------------------
 *
 * models product_model
 *
 * @package     models
 * @subpackage  
 * @category    product_model  -  产品模型类
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Product_model extends CI_Model
{    
	public static $error;
	const TABLE = 'xc_products'; // 表名
	private static $str_options; 	// 
	public function __construct()
	{
		parent::__construct();
        $this->load->database();
	}

	/**
	 * 增加数据
	 */
	public function insert($data)
	{
		if($this->db->insert(self::TABLE,$data)){
			return true;
		}else{
			return false;
		}
	}

             
	/**
	 * 查询所有的数据，并且分页
	 */
	public function get_all_data_and_page($limit)
	{
		$sql = 'select a.*,b.cate_name
		from '.self::TABLE.' as a left join xc_category as b
		on a.pro_pid=b.cate_id 
		order by a.pro_id desc '.$limit;
		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	/**
	 * 查询共有多少数据
	 */
	public function get_num_rows()
	{
		$query = $this->db->get(self::TABLE);
		return $query->num_rows();
	}

	/**
	 * 编辑数据信息
	 */
	public function update($data,$id)
	{
		if($this->db->update(self::TABLE,$data,'pro_id = '.$id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据id查询数据信息
	 */
	public function get_pro_by_aid($aid)
	{
		$this->db->where('pro_id',$aid);
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	/**
	 * 根据id删除数据
	 */
	public function delete($id)
	{
		$this->db->where('pro_id',$id);
		if($this->db->delete(self::TABLE)){
			return true;
		}else{
			return false;
		}
	}

	public function get_all_date(){
		$sql = 'select pro_id,pro_name,pro_sort from '.self::TABLE.' where pro_state = 1 order by pro_sort desc';
		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	public function to_options($selectval=null){
	    $arr = $this->get_all_date();
        if(is_array($arr)){
            foreach($arr as $rows){
                if($selectval == $rows['pro_id']){
                    self::$str_options.='<option value="'.$rows['pro_id'].'"  selected="selected" >|_&nbsp;'.$rows['pro_name'].'</option>';
                }else{
                   self::$str_options.='<option value="'.$rows['pro_id'].'"  >|_&nbsp;'.$rows['pro_name'].'</option>';
                }
            }
        }
       return self::$str_options;
	}



	public function get_pro_id_attr($pid){
		
	}

}